﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Threading;

namespace Kocic.RepositoryManagement.EntityFramework4
{
    /// <summary>
    /// Allows entity manager objects to re-use a single instance of an object context for the ADO.NET Entity Framework.
    /// </summary>
    /// <typeparam name="TContext">The type of the entity object context to use within the execution context of this instance.</typeparam>
    public class EntityExecutionContext<TContext> : DataSourceExecutionContext<TContext>
        where TContext : ObjectContext, new()
    {
        /// <summary>
        /// Creates an instance of the EntityExecutionContext class.
        /// </summary>
        public EntityExecutionContext()
        {

        }

        /// <summary>
        /// Disposes unmanaged resources.
        /// </summary>
        /// <param name="disposing">Value indicating if to invoke the method from the IDisposable.Dispose implementation or from the finalizer.</param>
        protected override void Dispose(Boolean disposing)
        {
            if (disposing)
            {
                if (this.Disposed)
                {
                    return;
                }

                ExecutionContext = null;

                if (this.DataSource.Connection.State == System.Data.ConnectionState.Open)
                {
                    this.DataSource.Connection.Close();
                }

                this.DataSource.Dispose();

                base.Dispose(disposing);
            }
        }
    }
}